package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
/**
 * Created by zzy on 2018/1/9.
 */
public class URLTest {

    public static void main(String[] args) throws IOException {
        //创建URL实力
        try {
            URL url=new URL("http://www.baidu.com");
            //URL url=new URL(imooc,"/index.htm?zzy=赵泽阳#test");//#标示锚点
            String xieyi= url.getProtocol();
            String host=url.getHost();
            //如果未指定端口号则，则使用默认端口号，此时getPort()返回值为-1；
            Integer port=url.getPort();
            String filePath=url.getPath();
            String fileName=url.getFile();
            String ref=url.getRef();
            String query=url.getQuery();
            Print.print(xieyi+":"+host+":"+port+":"+filePath+":"+fileName+":"+ref+":"+query);

            //字节
            InputStream stream= url.openStream();
            //字符
            InputStreamReader reader=new InputStreamReader(stream,"UTF-8");

            BufferedReader reader1=new BufferedReader(reader);

            String data=reader1.readLine();
            while (data!=null){
                Print.print(data);
                data=reader1.readLine();
            }
            reader1.close();
            reader.close();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
